package cn.tedu.servlet;

import cn.tedu.http.HttpRequest;
import cn.tedu.http.HttpResponse;
import cn.tedu.vo.User;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * Author：程Sir
 * Name： SmallServer
 * Content:
 *  用来处理查询用户数据的业务
 */
public class ShowAllDataServlet {

    public void service(HttpRequest httpRequest, HttpResponse httpResponse){
        System.out.println("开始用户数据查询的业务.....");
        // 读取用户的所有数据
        List<User> users = new ArrayList<>();
        // 创建对象
        try (RandomAccessFile raf = new RandomAccessFile("user.txt","r")){

            byte[] datas = new  byte[32];
            for (int i = 0; i < raf.length()/100; i++) {

                // 读取用户名
                raf.read(datas);
                String username = new String(datas,"utf-8").trim();
                // 读取密码
                raf.read(datas);
                String password = new String(datas,"utf-8").trim();
                // 读取昵称
                raf.read(datas);
                String nikename = new String(datas,"utf-8").trim();

                // 读取年龄
                int age = raf.readInt();

                // 将查询到的用户数据封装到user对象中
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setNikename(nikename);
                user.setAge(age);

                // 把查询到的user对象添加到集合中
                users.add(user);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("读取到的用户数据集合："+users);

        // 将获得的用户集合写出
        PrintWriter printWriter = httpResponse.getWriter();

        printWriter.println("<!DOCTYPE html>\n" );
        printWriter.println("<html lang=\"en\">\n");
        printWriter.println("<head>\n");
        printWriter.println("    <meta charset=\"UTF-8\">\n");
        printWriter.println("    <title>用户数据</title>\n");
        printWriter.println("</head>\n");
        printWriter.println("<body>\n");
        printWriter.println("<center>\n");
        printWriter.println("    <h1>用户列表</h1>\n");
        printWriter.println("    <table border=\"1\">\n");
        printWriter.println("        <tr>\n");
        printWriter.println("            <td>用户名</td>\n");
        printWriter.println("            <td>密码</td>\n");
        printWriter.println("            <td>昵称</td>\n");
        printWriter.println("            <td>年龄</td>\n");
        printWriter.println("            <td>操作</td>\n");
        printWriter.println("        </tr>\n");

        for (User user : users) {
            printWriter.println("        <tr>\n");
            printWriter.println("            <td>" + user.getUsername() + "</td>\n");
            printWriter.println("            <td>" + user.getPassword() + "</td>\n");
            printWriter.println("            <td>" + user.getNikename() + "</td>\n");
            printWriter.println("            <td>" + user.getAge() + "</td>\n");
            printWriter.println("            <td><a href='./update?username="+ user.getUsername() +"'>修改用户数据</a></td>\n");
            printWriter.println("        </tr>\n");
        }

        printWriter.println("    </table>\n" );
        printWriter.println("</center>\n" );
        printWriter.println("</body>\n");
        printWriter.println("</html>");

        System.out.println("用户数据查询的业务完毕！！");

    }



    public static void main(String[] args) {
        ShowAllDataServlet servlet = new ShowAllDataServlet();
        servlet.service(null,null);
    }
}
